﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*
 * 
*/

namespace Licenta_Stomatologie.Helpers
{
    class CompanyHelper
    {
        private ProjectDBEntities db = new ProjectDBEntities();
        private int companyId = 1;

        public CompanyHelper()
        {
            
        }

        public void createCompany(String[] args)
        {
            // Arguments order : Unitate sanitara, Judet, Localitate, Adresa, Nr. telefon, Adresa web, logo
            // Leave logo out for now
            if (!checkArguments(args))
            {
                System.Windows.Forms.MessageBox.Show(Helpers.ErrorMessages.ERR_ILLEGAL_ARGUMENTS);
                return;
            }

            if (db.Companies.Count() == 0)
            {
                Company comp = new Company();

                comp.unitateSanitara = args[0];
                comp.judet = args[1];
                comp.localitate = args[2];
                comp.adresa = args[3];
                comp.numarTelefon = args[4];
                comp.adresaWeb = args[5];
                comp.logo = "lalala";

                //db.Entry(comp).State = System.Data.EntityState.Added;
                db.Companies.Add(comp);
            }
            else
            {
                Company comp = db.Companies.First(c => c.id > 0);

                comp.unitateSanitara = args[0];
                comp.judet = args[1];
                comp.localitate = args[2];
                comp.adresa = args[3];
                comp.numarTelefon = args[4];
                comp.adresaWeb = args[5];
                comp.logo = "lalala";
            }

            db.SaveChanges();

            Console.WriteLine("Apparently it saved...");
        }

        private bool checkArguments(String[] arguments)
        {
            // Arguments order : Unitate sanitara, Judet, Localitate, Adresa, Nr. telefon, Adresa web, logo
            for (int i = 0; i < 6; i++)
            {
                if (arguments[i].Length == 0)
                {
                    return false;
                }
            }

                return true;
        }

        public String[] sendEditData()
        {
            if (db.Companies.Count() == 0)
                return null;

            Company comp = db.Companies.First(c => c.id > 0);

            return new String[] { comp.unitateSanitara, comp.judet, comp.localitate, comp.adresa, comp.numarTelefon, comp.adresaWeb };
        }

    }
}
